﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BaseClasses;
using SVAccountingBL;

namespace SVAccountingWebUI
{
    public partial class FinancialCards : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected override void EnableAddButton()
        {
            lbtnAddNew.Enabled = true;
        }
        protected override void DisableAddButton()
        {
            lbtnAddNew.Enabled = false;
        }

        protected override void EnableInsetDiv()
        {
            InsertDiv.Visible = true;
            dvInsertFinantialCard.ChangeMode(DetailsViewMode.Insert);
        }

        protected override void DisableInsetDiv()
        {
            InsertDiv.Visible = false;
            dvInsertFinantialCard.Controls.Clear();
        }

        protected override void SetControlsAfterInsert()
        {
            GridView1.DataBind();
            InsertDiv.Visible = false;
            dvInsertFinantialCard.Controls.Clear();
        }

        protected override void SetControlsBrforeInsert(LinqDataSourceInsertEventArgs e)
        {
            ((SVAccountingBL.FinancialCard)e.NewObject).DateCreated = DateTime.Now;
            ((SVAccountingBL.FinancialCard)e.NewObject).CreatorId = 4;

            DropDownList ddlComintentCompanies = (DropDownList)dvInsertFinantialCard.FindControl("ddlComintenttAccount");
            if (!string.IsNullOrEmpty(ddlComintentCompanies.SelectedValue))
                ((SVAccountingBL.FinancialCard)e.NewObject).ComintenttAccountID = int.Parse(ddlComintentCompanies.SelectedValue);

            DropDownList ddlOwnerAccount = (DropDownList)dvInsertFinantialCard.FindControl("ddlOwnerAccount");
            if (!string.IsNullOrEmpty(ddlOwnerAccount.SelectedValue))
                ((SVAccountingBL.FinancialCard)e.NewObject).OwnerAccountID = int.Parse(ddlOwnerAccount.SelectedValue);

            ((SVAccountingBL.FinancialCard)e.NewObject).Debit = 0;
            ((SVAccountingBL.FinancialCard)e.NewObject).Credit = 0;

        }


        protected void ddlCommitentCompanies_SelectedIndexChanged(object sender, EventArgs e)
        {
            SVALinqDataContext db = new SVALinqDataContext();
            DropDownList ddl = (DropDownList)sender;
            DropDownList ddlComintentAccount = (DropDownList)dvInsertFinantialCard.FindControl("ddlComintenttAccount");
            HtmlContainerControl divCombaniesAccount =  (HtmlContainerControl)dvInsertFinantialCard.FindControl("dvComintentAccount");


            if (ddl.SelectedIndex != -1 && int.Parse(ddl.SelectedValue) != -1)
            {
                var t = from q in db.CompanyAccounts
                        where q.CompanyID == int.Parse(ddl.SelectedValue)
                        select new
                        {
                            FullTextValue = q.BankName + "(" + q.AccountCode + ")",
                            FullValue = q.AccountID
                        };


                divCombaniesAccount.Visible = true;
                ddlComintentAccount.DataSource = t;
                ddlComintentAccount.DataTextField = "FullTextValue";
                ddlComintentAccount.DataValueField = "FullValue";
                ddlComintentAccount.DataBind();
            }
            else
                divCombaniesAccount.Visible = false;


        }
        protected void ddlOwnersCompanies_SelectedIndexChanged(object sender, EventArgs e)
        {
            SVALinqDataContext db = new SVALinqDataContext();
            DropDownList ddl = (DropDownList)sender;
            DropDownList ddlOwnerAccount = (DropDownList)dvInsertFinantialCard.FindControl("ddlOwnerAccount");
            HtmlContainerControl divOwnerAccount =  (HtmlContainerControl)dvInsertFinantialCard.FindControl("dvOwnerAccount");


            if (ddl.SelectedIndex != -1 && int.Parse(ddl.SelectedValue) != -1)
            {
                var t = from q in db.CompanyAccounts
                        where q.CompanyID == int.Parse(ddl.SelectedValue)
                        select new
                        {
                            FullTextValue = q.BankName + "(" + q.AccountCode + ")",
                            FullValue = q.AccountID
                        };


                divOwnerAccount.Visible = true;
                ddlOwnerAccount.DataSource = t;
                ddlOwnerAccount.DataTextField = "FullTextValue";
                ddlOwnerAccount.DataValueField = "FullValue";
                ddlOwnerAccount.DataBind();
            }
            else
                divOwnerAccount.Visible = false;


        }

        protected void dvInsertFinantialCard_ItemInserting(object sender, DetailsViewInsertEventArgs e)
        {
            SVAccountingBL.FinancialCard fc = new FinancialCard();

            fc.DateCreated = DateTime.Now;
            fc.CreatorId = 4;
            DropDownList ddlComintentCompanies = (DropDownList)dvInsertFinantialCard.FindControl("ddlComintenttAccount");
            if (!string.IsNullOrEmpty(ddlComintentCompanies.SelectedValue))
                fc.ComintenttAccountID = int.Parse(ddlComintentCompanies.SelectedValue);

            DropDownList ddlOwnerAccount = (DropDownList)dvInsertFinantialCard.FindControl("ddlOwnerAccount");
            if (!string.IsNullOrEmpty(ddlOwnerAccount.SelectedValue))
                fc.OwnerAccountID = int.Parse(ddlOwnerAccount.SelectedValue);

            fc.Debit = 0;
            fc.Credit = 0;

            SVALinqDataContext db = new SVALinqDataContext();
            db.FinancialCards.InsertOnSubmit(fc);
            db.SubmitChanges();
        }

        protected void dvInsertFinantialCard_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
        {
            if (e.Exception == null)
            {
                SetControlsAfterInsert();
                ((Label)Master.FindControl("lblMasterMessage")).Text = GetGlobalResourceObject("Resource", "InsertSuccess").ToString();
                ((Label)Master.FindControl("lblMasterMessage")).ForeColor = System.Drawing.Color.Green;
            }
            else
            {
                ((Label)Master.FindControl("lblMasterMessage")).Text = GetGlobalResourceObject("Resource", "InsertError").ToString();
                ((Label)Master.FindControl("lblMasterMessage")).ForeColor = System.Drawing.Color.Red;
                e.ExceptionHandled = true;
            }
        }

        protected void dvInsertFinantialCard_ModeChanging(object sender, DetailsViewModeEventArgs e)
        {

        }

        protected void dvInsertFinantialCard_ItemCreated(object sender, EventArgs e)
        {

        }


        

    }
}
